package com.rachel.utilslib.utils;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * 这个工具类，实现的是沉浸式，透明栏等作用
 * Created by Administrator on 2017/10/19.
 */

public class StatusUtils {

    public static void setStatusColor(Activity activity,int color){
        Window window = activity.getWindow();
        // 4.4 到 5.0 的做法
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
                Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP){
            //设置透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //生成一个 状态栏大小的 view
            View view = createStatusBarView(activity,color);

            //添加到布局中
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(view);




        }else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){

            window.addFlags(
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //先清除 FLAG_TRANSLUCENT_STATUS 标志
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(color);
        }
    }


    /**
     * 获取状态栏高度
     * @param activity
     * @return
     */
    public static int getStatusBarHeight(Activity activity){
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0){

            return  activity.getResources().getDimensionPixelSize(resourceId);
        }
        return -1;
    }

    /**
     * 生成一个和状态大小一致的view
     * @param activity
     * @param color
     * @return
     */
    public static View createStatusBarView(Activity activity,int color) {
        // 绘制一个和状态栏一样高的矩形
        View view = new View(activity);
        ViewGroup.LayoutParams params =
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        getStatusBarHeight(activity));
        view.setLayoutParams(params);
        view.setBackgroundColor(color);
        return view;
    }
}
